const handler = require('sv-handle-res')

const {
  professionList,
  professionAdd,
  professionUpdate,
  professionDelete
} = require('./module/profession/index.js')

const {
  libList,
  libAdd,
  libUpdate,
  libDelete
} = require('./module/lib/index.js')

const {
  examList,
  examAdd,
  examUpdate,
  examDelete
} = require('./module/exam/index.js')

const {
  recordList,
  recordAdd,
  favLibList,
  favLibAdd,
  favLibDelete,
  favLibClear,
  buyLibList,
  buyLibAdd,
  favExamList,
  favExamAdd,
  favExamDelete,
  favExamClear,
  errorExamList,
  errorExamAdd,
  errorExamDelete,
  errorExamClear,
  doneExamList,
  doneExamAdd,
  correctNumQuery,
  correctNumInc,
  errorNumQuery,
  errorNumInc
} = require('./module/record/index.js')

const {
  feedbackList,
  feedbackAdd,
} = require('./module/feedback/index.js')

const {
  danmakuList,
  danmakuAdd
} = require('./module/danmaku/index.js')

module.exports = {
  _before: async function() { // 通用预处理器
    // // token身份安全校验
    // const WHITE_LIST = [] // 校验白名单
    // const apiPath = this.getHttpInfo().path
    // // 不是白名单的api需要进行校验
    // if (!WHITE_LIST.includes(apiPath)) {
    //   // 自动判断是否开启严格模式，路径包含List默认不开启，其他诸如Add、Update、Delete等皆开启
    //   const cToken = await handler.checkToken({
    //     clientInfo: this.getClientInfo(),
    //     token: this.getHttpInfo().headers.authorization,
    //     strict: false
    //   })
    //   if (cToken.code !== 200) {
    //     throw cToken
    //   }
    // }

    this.params = {} // 初始化参数
    this.startTime = Date.now() // 在before内记录开始时间并在this上挂载，以供后续流程使用
    // 使用 content-type: application/json 的请求头，参数在body中
    const body = this.getHttpInfo().body
    this.params = JSON.parse(body)
  },
  _after(error, result) {
    if (error) {
      throw error // 如果方法抛出错误，也直接抛出不处理
    }
    result.timeCost = Date.now() - this.startTime
    return result
  },

  /**
   * 职业
   */
  professionList,
  professionAdd,
  professionUpdate,
  professionDelete,

  /**
   * 题库
   */
  libList,
  libAdd,
  libUpdate,
  libDelete,

  /**
   * 题目
   */
  examList,
  examAdd,
  examUpdate,
  examDelete,

  /**
   * 记录
   */
  recordList,
  recordAdd,
  favLibList,
  favLibAdd,
  favLibDelete,
  favLibClear,
  buyLibList,
  buyLibAdd,
  favExamList,
  favExamAdd,
  favExamDelete,
  favExamClear,
  errorExamList,
  errorExamAdd,
  errorExamDelete,
  errorExamClear,
  doneExamList,
  doneExamAdd,
  correctNumQuery,
  correctNumInc,
  errorNumQuery,
  errorNumInc,

  /**
   * 弹幕
   */
  danmakuList,
  danmakuAdd,
  
  /**
   * 反馈
   */
  feedbackList,
  feedbackAdd,
}